/*******************************************************************************
 * Copyright 2013 Mindaugas Idzelis
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package incontrol.bf3.protocol.events.datatype;

import static com.google.common.collect.Iterables.toArray;
import static java.util.Arrays.asList;

import com.google.common.base.Objects;

public class PlayerSet {

	public static final PlayerSet ALL = new PlayerSet(SetType.ALL);

	private int team;
	private int squad;
	private String player;
	private SetType type;

	enum SetType {
		ALL, TEAM, SQUAD, PLAYER
	}

	public PlayerSet(SetType type) {
		this.type = type;
	}

	public PlayerSet(SetType type, int teamId) {
		this.type = type;
		this.team = teamId;
	}

	public PlayerSet(SetType type, int teamId, int squadId) {
		this.type = type;
		this.team = teamId;
		this.squad = squadId;
	}

	public PlayerSet(SetType type, String playerName) {
		this.type = type;
		this.player = playerName;
	}

	public int getTeam() {
		return team;
	}

	public int getSquad() {
		return squad;
	}

	public String getPlayer() {
		return player;
	}

	public SetType getType() {
		return type;
	}

	public static PlayerSet parse(String[] words) {
		String type = words[0];
		if ("all".equals(type)) {
			return new PlayerSet(SetType.ALL);
		} else if ("team".equals(type)) {
			int teamId = Integer.parseInt(words[1]);
			return new PlayerSet(SetType.TEAM, teamId);
		} else if ("squad".equals(type)) {
			int teamId = Integer.parseInt(words[1]);
			int squadId = Integer.parseInt(words[2]);
			return new PlayerSet(SetType.SQUAD, teamId, squadId);
		} else if ("player".equals(type)) {
			String playerName = words[1];
			return new PlayerSet(SetType.PLAYER, playerName);
		} else {
			throw new UnsupportedOperationException();
		}
	}

	public String[] getBody() {

		switch (getType()) {
		case ALL:
			return toArray(asList("all"), String.class);
		case PLAYER:
			return toArray(asList("player", player), String.class);
		case SQUAD:
			return toArray(
					asList("squad", Integer.toString(getTeam()),
							Integer.toString(getSquad())), String.class);
		case TEAM:
			return toArray(asList("team", Integer.toString(getTeam())),
					String.class);
		default:
			throw new RuntimeException("Invalid set");
		}
	}

	@Override
	public String toString() {
		if (getType() == SetType.ALL) {
			return Objects.toStringHelper(this).add("type", type).toString();
		} else if (getType() == SetType.TEAM) {
			return Objects.toStringHelper(this).add("type", type)
					.add("team", team).toString();
		} else if (getType() == SetType.SQUAD) {
			return Objects.toStringHelper(this).add("type", type)
					.add("team", team).add("squad", squad).toString();
		} else if (getType() == SetType.PLAYER) {
			return Objects.toStringHelper(this).add("type", type)
					.add("team", team).add("squad", squad)
					.add("player", player).toString();
		} else {
			return Objects.toStringHelper(this).add("type", type)
					.add("team", team).add("squad", squad)
					.add("player", player).toString();
		}

	}

}
